//
//  PersonView.swift
//  GhostWorld
//
//  Created by Kam on 2018/4/5.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit
import AlamofireImage

class PersonView: UIView {
  
  // MARK: - IBOutlets
  
  @IBOutlet private weak var avatarImageView: UIImageView!
  @IBOutlet private weak var nameLabel: UILabel!
  @IBOutlet private weak var avatarContainer: UIView!
  
  // MARK: - Private vars
  
  
  
  // MARK: - Private methods
  
  
  
  // MARK: - Public vars
  
  
  
  // MARK: - Lifecycle
  
  override func awakeFromNib() {
    super.awakeFromNib()
    
    backgroundColor = .clear
    
    // round avatar
    avatarImageView.layer.cornerRadius = 30
    avatarImageView.layer.masksToBounds = true
    avatarImageView.clipsToBounds = true;
    
    // add shadow
    avatarContainer.layer.shadowColor = UIColor.black.cgColor
    avatarContainer.layer.shadowOffset = CGSize.zero
    avatarContainer.layer.shadowOpacity = 0.4
    avatarContainer.layer.shadowRadius = 5
    avatarContainer.layer.shadowPath = UIBezierPath(roundedRect: avatarContainer.bounds, cornerRadius: 30).cgPath
    
    // add stroke
    avatarContainer.layer.cornerRadius = 30
    avatarContainer.layer.borderColor = UIColor(fromRGB: 0xCBCBCB).cgColor
    avatarContainer.layer.borderWidth = 1
  }
  
  // MARK: - IBActions
  
  
  
  // MARK: - Public methods
  
  func prepare(person: Any) {
    if type(of: person) == Writer.self {
      let writer = person as! Writer
      if let url = writer.avatar.getURL(size: .thumb) {
        avatarImageView.af_setImage(withURL: url)
      }
      nameLabel.text = writer.name
    } else if type(of: person) == Reader.self {
      let reader = person as! Reader
      if let url = reader.avatar.getURL(size: .thumb) {
        avatarImageView.af_setImage(withURL: url)
      }
      nameLabel.text = reader.name
    }
  }
}

extension PersonView {
  static var defaultHeight: CGFloat = 100
  static var defaultWidth: CGFloat = 60
}
